﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace xiaolanshu
{
    public partial class 选包装 : Form
    {
        public 选包装()
        {
            InitializeComponent();
        }
        private SqlConnection conn = SQLTools.conn;

        private void 选包装_Load(object sender, EventArgs e)
        {
            BackgroundImageLayout = ImageLayout.Stretch;
            uiLabel1.Text = "请选择您心仪的包装：";
            string sql = @"select pname from 包装";
            SqlCommand cmd = new SqlCommand(sql, conn);
            if (cmd.Connection.State == ConnectionState.Open)
            {
                cmd.Connection.Close();
            }
            conn.Open();
            SqlDataReader da = cmd.ExecuteReader();
            while(da.Read())
            {
                comboBox1.Items.Add(da[0]);
            }
            da.Close();
            conn.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = @"select pphoto from 包装 where pname ='"+comboBox1.Text+"'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["pphoto"]);
            Image i = Image.FromStream(ms, true);
            pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = i;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton1_Click(object sender, EventArgs e)
        {
            用户信息.pname = comboBox1.Text;

            SqlConnection con = SQLTools.conn;
            string sql = @"select * from 自定义产品 where 1=0";
            string sqll = @"select count(*) from 自定义产品;";
            //SqlCommand cmd = new SqlCommand(sqll, con);
            SqlDataAdapter daa = new SqlDataAdapter(sqll,con);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            SqlCommandBuilder builder = new SqlCommandBuilder(da);

            DataTable dataTable = new DataTable();
            DataTable table = new DataTable();
            daa.Fill(table);
            da.Fill(dataTable);

            DataRow row = dataTable.NewRow();
            row["productnum"] = table.Rows[0][0];
            row["color_p"] = 用户信息.color_p;
            row["color_o"] = 用户信息.color_o;
            row["color_pink"] = 用户信息.color_pink;
            row["package"] = 用户信息.pname;
            row["telephone"] = 用户信息.telephone;
            dataTable.Rows.Add(row);

            builder.GetUpdateCommand();
            da.Update(dataTable);
            MessageBox.Show("提交成功！我们将为您定制该产品！");
            this.Close();
        }
    }
}
